<template>
  <div style="min-width: 1100px">
    <div class="licc-order-deliver-info-top">
      <el-form :model="filterOrderList" ref="filterOrderList"  label-width="80px">
        <el-row>
          <el-col :span="5">
            <el-form-item
              label=""
              prop="orderSn"
              :rules="[{trigger: 'blur', validator: validateOrderSnRule}]">
              <el-input v-model="filterOrderList.orderSn" placeholder="请输入订单号"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item
              label=""
              prop="phone"
              :rules="[{trigger: 'blur', validator: validateOrderSnRule}]">
              <el-input v-model="filterOrderList.phone" placeholder="购买人手机号"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label=下单时间>
              <el-date-picker
                v-model="datePickerValue"
                value-format="yyyy-MM-dd HH:mm:SS"
                :picker-options="pickerOptions"
                type="daterange"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="4" :offset="2">
            <el-button type="info" @click="restOrderInfosButton">重 置</el-button>
            <el-button type="primary" @click="selectOrderInfosButton">筛 选</el-button>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <div>
      <el-table
        stripe
        border
        :data="orderInfos"
        :default-sort = "{prop: 'createDate', order: 'descending'}">
        <el-table-column
          type="index"
          width="50">
        </el-table-column>
        <el-table-column prop="orderSn" label="订单编号" sortable></el-table-column>
        <el-table-column prop="createDate" label="下单时间"  sortable></el-table-column>
        <el-table-column prop="orderStatus" label="状态">
            <el-tag type="danger">待发货</el-tag>
        </el-table-column>
        <el-table-column prop="shopUser.phone" label="购买人手机号"></el-table-column>
        <el-table-column prop="goodsAmount" label="金额（元）" ></el-table-column>
        <el-table-column prop="consignee" label="收货人"></el-table-column>
        <el-table-column prop="phone" label="收货人手机号"></el-table-column>
        <el-table-column label="收货人地址">
          <template slot-scope="scope">
            {{initAddressInfo(scope.row)}}
          </template>
        </el-table-column>
        <el-table-column prop="postscript" label="订单附言"></el-table-column>
        <el-table-column prop="payTime" label="支付时间"></el-table-column>
        <el-table-column fixed="right" width="185" label="操作">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="primary"
              icon="el-icon-tickets"
              :disabled="buttonDisabled(scope.row.orderStatus, 1)"
              @click="handleInfo(scope.$index, scope.row)">详情</el-button>
            <el-button
              size="mini"
              type="primary"
              icon="el-icon-tickets"
              :disabled="buttonDisabled(scope.row.orderStatus, 1)"
              @click="handleNext(scope.$index, scope.row)">发货</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div>
      <div class="block">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[10, 20, 35, 50]"
          :page-size="pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="goodsTotal">
        </el-pagination>
      </div>
    </div>

    <!-- 详细信息 -->
    <div>
      <el-dialog :title="orderInfoDialogTitle" :visible.sync="dialogOrderInfoVisible" width="60%">
        <span>
          <order-info v-if="dialogOrderInfoVisible" :message="orderAllInfo"></order-info>
        </span>
        <span slot="footer" class="dialog-footer">
          <el-button type="primary" @click="dialogOrderInfoVisible = false">返 回</el-button>
        </span>
      </el-dialog>
    </div>
    <div>
      <el-dialog
        :title="orderInfoDialogTitle"
        :visible.sync="dialogOrderNextVisible"
        width="30%"
        :before-close="orderNextClose">
        <el-form :model="orderNextInfo" :rules="orderNextInfoRules" ref="orderNextInfo" class="licc-order-deliver-info-form" label-width="95px">
          <el-form-item label="发货单号：" prop="invoiceNo">
            <el-input name="invoiceNo" v-model="orderNextInfo.invoiceNo" placeholder="输入发货单号"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
        <el-button @click="dialogOrderNextVisible = false">取 消</el-button>
        <el-button type="primary" @click="orderNextButton">确 定</el-button>
      </span>
      </el-dialog>
    </div>
  </div>
</template>

<script>
  import orderApi from '@/api/backstage/order'
  import OrderInfo from '../components/orderInfo'
  export default {
    name: 'Goods_listInfo',
    components: {
      OrderInfo
    },
    data() {
      const validateOrderInvoice = (rule, value, callback) => {
        if (value === ''||typeof(value)=="undefined") {
          callback(new Error('请输入发货单号'));
        }
        let emailRule = /^[a-zA-Z0-9_][a-zA-Z0-9_]{4,19}$/
        if (!emailRule.test(value)){
          callback(new Error('发货单号由5-20字符下划线、数字或字母组成'));
        }
        callback();
      }
      return {
        /** 筛选条件 **/
        filterOrderList:{
          orderSn:'',
          phone:'',
        },
        /** 日期选择器 **/
        datePickerValue:'',
        pickerOptions: {
          disabledDate(time) {
            return time.getTime() > Date.now() - 8.64e6
          }
        },
        /** 订单信息 **/
        orderInfos:[],
        currentPage: 1,
        pagesize:20,
        goodsTotal:0,
        /** 订单状态 **/
        initStatus:[30],
        /** 详细信息不显示 **/
        dialogOrderInfoVisible: false,
        /** 发货页面 **/
        dialogOrderNextVisible:false,
        /** 订单详细信息 **/
        orderAllInfo:{
          goodsAmount:'',
          shopUser:{phone:''},
          consignee:'',
          phone:'',
          province:'',
        },
        /** 订单详情显示标题 **/
        orderInfoDialogTitle:'',
        /** 订单发货界面信息 **/
        orderNextInfo:{
          invoiceNo:''
        },
        /** 订单发货规则 **/
        orderNextInfoRules:{
          invoiceNo:[{required: true, trigger: 'blur', validator: validateOrderInvoice}],
        },
      }
    },
    created() {
      this.initData();
    },
    methods: {
      initData() {
        let params = {
          orderStatusList: this.initStatus,
          currentPage: this.currentPage,
          pagesize: this.pagesize,
          orderSn: this.filterOrderList.orderSn,
          phone: this.filterOrderList.phone,
          createDate:this.datePickerValue
        }
        orderApi.initOrderList(params).then(res => {
          this.orderInfos = res.data.records;
          this.goodsTotal = res.data.total;
        }).catch(() => {
          this.$notify.error({title: '失败', message: "获取用户信息失败"});
        })
      },
      /** 分页---开始 **/
      handleSizeChange(val) {
        this.pagesize = val;
        this.initData();
      },
      handleCurrentChange(val) {
        this.currentPage = val;
        this.initData();
      },
      /** 分页---结束 **/
      /** 详细地址信息 **/
      initAddressInfo(row){
        return row.province+row.city+row.district+row.street+"-"+row.address;
      },
      /** 筛选订单编号数字 **/
      validateOrderSnRule(rule, value, callback){
        let orderSNRule = /^[0-9]*$/;
        if (!orderSNRule.test(value)) {
          console.log(value);
          callback(new Error('只能由数字组成'));
        } else {
          callback()
        }
      },
      /** 重置搜索 **/
      restOrderInfosButton(){
        this.filterOrderList.orderSn = '';
        this.datePickerValue = null;
        this.filterOrderList.orderSn = '';
        this.filterOrderList.phone = '';
        this.$refs['filterOrderList'].clearValidate();
        this.currentPage = 1;
        this.initData();
      },
      /** 条件筛选 **/
      selectOrderInfosButton(){
        this.$refs['filterOrderList'].validate(valid => {
          if (valid){
            this.currentPage = 1;
            this.initData();
          } else {
            this.$notify({title: '提示', message: '输入的信息不合规', type: 'warning'});
          }
        });
      },

      /** 操作按钮点击事件 **/
      handleInfo(orderIndex, row){
        /** 订单详情 **/
        this.orderAllInfo = {};
        this.orderAllInfo = row;
        /** 订单详情标题 **/
        this.orderInfoDialogTitle = "订单号："+row.orderSn+" - 时间："+row.createDate+" - 状态：待发货";
        /** 详细信息显示 **/
        this.dialogOrderInfoVisible = true;
      },
      /** 列表页面发货按钮 **/
      handleNext(orderIndex, row){
        /** 发货订单信息赋值 **/
        this.orderNextInfo = row;
        this.orderNextInfo.invoiceNo = '';
        /** 设置发货页面标题 **/
        this.orderInfoDialogTitle = "订单号："+row.orderSn;
        this.$nextTick(function () {
          /** 清楚发货界面样式 **/
          this.$refs['orderNextInfo'].clearValidate();
        });
        /** 发货界面显示 **/
        this.dialogOrderNextVisible = true;
      },
      /** 操作按钮显示 **/
      buttonDisabled(status, type){
      },
      /** 发货界面发货按钮 **/
      orderNextButton(){
        this.$refs['orderNextInfo'].validate(valid => {
          if (valid){
            orderApi.deliverOrderNext({id: this.orderNextInfo.id,invoiceNo: this.orderNextInfo.invoiceNo,nowTime: this.getNowFormatDate()}).then(res => {
              this.dialogOrderNextVisible = false;
              this.$notify.success({title:'成功',message:'发货成功！'});
              this.initData();
            }).catch(() => {
              this.$notify.error({title: '失败', message: '发货失败，请稍后再试'});
            })
          } else {
            this.$notify({title: '提示', message: '输入的信息不合规', type: 'warning'});
          }
        });
      },
      /** 发货页面关闭 **/
      orderNextClose(done) {
        this.$confirm('确认取消发货？').then(_ => {
            done();
        }).catch(_ => {});
      },
      getNowFormatDate() {
        let date = new Date();
        let seperator1 = "-";
        let seperator2 = ":";
        let month = date.getMonth() + 1;
        let strDate = date.getDate();
        let HH = date.getHours();
        let mm = date.getMinutes();
        let ss = date.getSeconds();
        if (month >= 1 && month <= 9) {
          month = "0" + month;
        }
        if (strDate >= 0 && strDate <= 9) {
          strDate = "0" + strDate;
        }
        if (HH >= 0 && HH <= 9){
          HH = "0" + HH;
        }
        if (mm >= 0 && mm <= 9){
          mm = "0" + mm;
        }
        if (ss >= 0 && ss <= 9){
          ss = "0" + ss;
        }
        let currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate
          + " " + HH + seperator2 + mm + seperator2 + ss;
        return currentdate;
      }
    }
  }
</script>

<style>
  .licc-order-deliver-info-top{
    width: 100%;
    min-width: 1100px;
  }
  .licc-order-deliver-info-form>.el-form-item{
    margin-bottom: 0px;
  }
</style>
